package com.lvchenglong;

import com.lvchenglong.configcenter.TestConfiguration;
import com.lvchenglong.configcenter.TestConfigurationByAnnotation;
import com.lvchenglong.listener.MyApplicationListener;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.Arrays;

@SpringBootApplication
public class Application{

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.addListeners(new MyApplicationListener());
        ConfigurableApplicationContext context = app.run(args);

        // test get bean
        String value = (String) context.getBean("testname");
        System.out.println("get bean test:" + value);

        // test configuration
        TestConfiguration config = context.getBean(TestConfiguration.class);
        System.out.println("configuration value test:" + config.getName());
        System.out.println("configuration value  random:" + config.myint);

        // test configuration
        TestConfigurationByAnnotation testConfigurationByAnnotation = context.getBean(TestConfigurationByAnnotation.class);
        System.out.println("ConfigurationProperties test:" + testConfigurationByAnnotation.getName());
        System.out.println("ConfigurationProperties test random:" + testConfigurationByAnnotation.number);
    }

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

//            String[] beanNames = ctx.getBeanDefinitionNames();
//            Arrays.sort(beanNames);
//            for (String beanName : beanNames) {
//                System.out.println("bean:" + beanName);
//            }

        };
    }
}
